www.gusucode.com > 支持向量机工具箱 - LIBSVM OSU_SVM LS_SVM源码程序 > 支持向量机工具箱 - LIBSVM OSU_SVM LS_SVM\stprtool\svm\svmlight.m

    function [Alpha,bias,nsv,kercnt,trnerr,margin]=svmlight(data,labels,ker,arg,C,eps)
% SVMLIGHT interface to the SVM^light software.
% 
% [Alpha,bias,nsv,kercnt,trnerr,margin]=svmlight(data,labels,ker,arg,C,eps)
%
% The programs 'svm_learn' and 'svm_classify' must be in the path.
%
% Inputs:
%  data [dim x N] training patterns
%  labels [1 x N] labels of training patterns
%  ker [string] kernel, see 'help kernel'.
%  arg [...] argument of given kernel, see 'help kernel'.
%  C [real] trade-off between margin and training error.
%  eps [real] KKT stopping condiiton.
%  verb [int] if 1 then progress info is displayed. 
% 
% Outputs:
%  Alpha [1 x N] found Lagrangeian multipliers.
%  bias [real] found bias.
%  nsv [real] number of Support Vectors (number of Alpha > ZERO_LIM).
%  kercnt [int] number of kernel evalutions.
%  trnerr [real] classification error on training data.
%  margin [real] margin between classes and the found hyperplane.
%

%  Statistical Pattern Recognition Toolbox, Vojtech Franc, Vaclav Hlavac
%  (c) Czech Technical University Prague, http://cmp.felk.cvut.cz
%  Written Vojtech Franc (diploma thesis) 02.11.1999, 13.4.2000
%
% Modifications
%  26-sep-2002, VF
%  3-Jun-2002, V.Franc

[dim,num_data ] = size(data);

if nargin < 6,
  eps=0.001;
end

if nargin < 5,
  error('Not enough input arguments.');
end

%--------------------------------
switch ker
  case 'linear'
    ker='-t 0';
  case 'rbf'
    ker=['-t 2 -g ' num2str(1/(2*arg^2))]; 
  case 'poly' 
    ker=['-t 1 -r 1 -s 1 -d ' num2str(arg)];  
end

command=['svm_learn ' ...
         '-c ' num2str(C) ' '...
         ker ' '...
         '-v 1' ' ' ...
         '-m 1' ' ' ...
         '-e ' num2str(eps) ' '...
         '-a tmp_alpha.txt tmp_examples.txt tmp_model.txt > tmp_verb.txt'];
   
    
xi2svmlight(data,labels,'tmp_examples.txt');
    
    % call SVM_LIGHT
%    evalc(command);
    [a,b]=unix(command);
    
    [lines]=textread('tmp_model.txt','%s');
    for i=1:size(lines,1)
      if strcmpi( lines(i), 'threshold' )==1,
        bias=-str2num( lines{i-2});
        break;
      end
    end
    
    Alpha=textread('tmp_alpha.txt','%f');
    Alpha=Alpha(:)'.*itosgn(labels);

    [lines]=textread('tmp_verb.txt','%s');
    for i=1:size(lines,1)
      if strcmpi( lines{i}, 'misclassified,' ),
        trnerr=str2num( lines{i-1}(2:end));
        trnerr=trnerr/length(Alpha);
      end
      if strcmpi( lines(i), 'vector:' ) & strcmpi( lines(i-1), 'weight' )==1,
        margin=1/str2num( lines{i+1}(5:end));
      end
      if strcmpi( lines(i), 'SV:' )==1,
        nsv=str2num( lines{i+1});
      end
      if strcmpi( lines(i), 'evaluations:' )==1,
        kercnt=str2num( lines{i+1});
      end
    end
    
% Alpha;
% bias;
% kercnt;
% margin;
    nsv = length(find(Alpha~=0));
% trnerr;


return;

%EOF